package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

/**
 * 利用Lombok生成Model
 * @author guobingbing
 * @since 2023/6/12 11:43
 */
public class LombokPlugin extends PluginAdapter {

	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		//添加domain的import
		topLevelClass.addImportedType("lombok.Data");
		topLevelClass.addImportedType("lombok.EqualsAndHashCode");

		//添加domain的注解
		topLevelClass.addAnnotation("@EqualsAndHashCode(callSuper = true)");
		topLevelClass.addAnnotation("@Data");

		return true;
	}

	/**
	 * 为字段增加注解
	 * @author guobingbing
	 * @since 2023/6/12 11:48
	 * @param field
	 * @param topLevelClass
	 * @param introspectedColumn
	 * @param introspectedTable
	 * @param modelClassType
	 */
//	@Override
//	public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
//	                                   IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//		field.addJavaDocLine("/**");
//		String remarks = introspectedColumn.getRemarks();
//		if (StringUtility.stringHasValue(remarks)) {
//			String[] remarkLines = remarks.split(System.getProperty("line.separator"));
//			for (String remarkLine : remarkLines) {
//				field.addJavaDocLine(" * " + remarkLine);
//			}
//		}
//		field.addJavaDocLine(" */");
//		return true;
//	}

	/**
	 * 不生成getter
	 * @author guobingbing
	 * @since 2023/6/12 11:48
	 * @param method
	 * @param topLevelClass
	 * @param introspectedColumn
	 * @param introspectedTable
	 * @param modelClassType
	 */
	@Override
	public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass,
	                                          IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
		return false;
	}

	/**
	 * 不生成setter
	 * @author guobingbing
	 * @since 2023/6/12 11:49
	 * @param method
	 * @param topLevelClass
	 * @param introspectedColumn
	 * @param introspectedTable
	 * @param modelClassType
	 */
	@Override
	public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass,
	                                          IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
		return false;
	}
}
